<?php

namespace catchAdmin\basics\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catcher\CatchAuth;
use catchAdmin\basics\model\InsideType as InsideTypeModel;

class InsideType extends CatchController
{
    
    protected $insideTypeModel;
    
    /**
     *
     * @time 2022/02/28 16:55
     * @param InsideTypeModel $insideTypeModel
     * @return mixed
     */
    public function __construct(InsideTypeModel $insideTypeModel)
    {
        $this->insideTypeModel = $insideTypeModel;
    }

    /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->insideTypeModel->getList($user['tenant_id']));
    }

    /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,Request $request)
    {
        $params = $request->filterEmptyField()->post();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        return CatchResponse::success($this->insideTypeModel->storeBy($params),"新建成功");
    }

    /**
     *
     * @time 2022/02/28 16:55
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $params = $request->filterEmptyField()->post();
        return CatchResponse::success($this->insideTypeModel->updateBy($params['id'], $params),"修改成功");
    }

    /**
     * 删除
     * @time 2022/02/28 15:16
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
         $id = $request->post('id');
         return CatchResponse::success($this->insideTypeModel->deleteBy($id),'删除成功');
    }

    /**
     *
     * @time 2022/02/28 15:16
     * @param Request $request
     * @return \think\response\Json
     */
    public function getInsideType(Request $request)
    {
         $type = $request->post('type');
         $where = [];
         $where[] = ['deleted_at','=',0];
         $where[] = ['type','in',$type];
         $data = $this->insideTypeModel->where($where)->order('id','desc')->field('id,name')->select();
         return CatchResponse::success($data,'查询成功');
    }
}